//
//  FormProtocol.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/12/12.
//

import Foundation
import UIKit

// MARK: - 动态绑定key
private struct AssociateKeys {
    // 提交数据
    static var dataKey: Void? /// 参数key
}

//MARK: - 表单每个cell的提交协议
public protocol FormResultProtocol { }


public extension FormResultProtocol where Self: TableViewModelProtocol {
    
    /// 是否完全可提交
    @discardableResult
    func checkSumbit(_ errorInfoBlock: ((_ msg: String) -> Void)? = nil) -> Dictionary<String, Any>? {
        var submitData: Dictionary<String, Any> = [:]
        var isBreak = false
        self.sections.forEach { section in
            if isBreak { return }
            section.items.forEach { row in
                if isBreak { return }
                guard let item = row as? FormModelProtocol else {
                    return
                }
                if item.isReady() {
                    submitData = submitData.add(item.sumbitData()!)
                }else {
                    if errorInfoBlock != nil {
                        errorInfoBlock!(item.errorInfo())
                    }
                    isBreak = true
                }
            }
        }
        if isBreak {
            return nil
        }
        return submitData
    }
    
    /// 当前可提交的参数
    func currentParams() -> Dictionary<String, Any> {
        var submitData: Dictionary<String, Any> = [:]
        self.sections.forEach { section in
            section.items.forEach { row in
                guard let item = row as? FormModelProtocol else {
                    return
                }
                if item.isReady() {
                    submitData = submitData.add(item.sumbitData()!)
                }
            }
        }
        return submitData
    }
    
    /// 当前可提交的参数
    func currentImageSources() -> [[String: [UIImage]]] {
        var imagesDicts: [[String: [UIImage]]] = []
        currentImageCotrol().forEach { dict in
            var lastDict: [String: [UIImage]] = [:]
            dict.keys.forEach { key in
                var imageArrray: [UIImage] = []
                dict[key]?.forEach{ contorl in
                    if let source = contorl.imageSource {
                        imageArrray.append(source)
                    }
                }
                lastDict[key] = imageArrray
            }
            imagesDicts.append(lastDict)
        }
        return imagesDicts
    }
    
    func currentImageCotrol() -> [[String: [ViewControlProtocol]]] {
        var imagesDictArray: [[String: [ViewControlProtocol]]] = []
        self.sections.forEach { tableSection in
            tableSection.items.forEach { tableRow in
                guard let cellConfig = tableRow as? TableShowCollectionViewCellModel else { // 是包含collection的cell
                    return
                }
                guard let cvcVM = cellConfig.model as? CollectionViewBaseVM else { // cell vm 是包含 默认 cvc vm 的
                    return
                }
                cvcVM.sections.forEach { colletionSection in
                    var sectionDict: [String: [ViewControlProtocol]] = [:]
                    var imageArray: [ViewControlProtocol] = []
                    colletionSection.items.forEach { collectionItem in
                        if let control = collectionItem.model as? ViewControlProtocol {
                            imageArray.append(control)
                        }
                    }
                    sectionDict[cvcVM.fileKey] = imageArray
                    imagesDictArray.append(sectionDict)
                }
            }
        }
        return imagesDictArray
    }
    
}

